﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Dmo_lc_2.Utils;

namespace Dmo_lc_2.Shape
{
    public class BaseShape
    {
        public enum ShapeType
        {
            BASE,
            LINE,
            CIRCLE,
            ARC,
            POLYLINE,
            DBTEXT,
            BLOCKREFERENCE,
            HATCH,
            MTEXT,
            ELLIPSE,
            ALIGNEDDIMENSION,
            ROTATEDDIMENSION,
            LINEANGULARDIMENSION2,
            SOLID,
            MLEADER,
            MLINE,
        }
        public int blockId;

        [Newtonsoft.Json.JsonIgnore()]
        protected Entity mEntity;

        [Newtonsoft.Json.JsonIgnore()]
        protected LayerTableRecord mLayer;

        [Newtonsoft.Json.JsonIgnore()]
        protected Transaction mTransaction;

        public ShapeType shapeType = ShapeType.BASE;
        public string shapeTypeName = ShapeType.BASE.ToString();

        public int[] color;
        public List<Dictionary<string, object>> lineTypeList;
        public double[] bound;

        public int lineWeight;

        public BaseShape(Entity in_entity, Transaction tr)
        {
            blockId = 101;
            mEntity = in_entity;
            mTransaction = tr;
            mLayer = (LayerTableRecord)mTransaction.GetObject(in_entity.LayerId, OpenMode.ForRead);
            Init();
            lineTypeList = getLineType(mEntity.LinetypeId ,mTransaction , mEntity.LayerId , mEntity.BlockId);
            getLineWeight();
            bound = getBounds();
        }

        public virtual void Init() { }

        public BaseShape getObject()
        {
            return this;
        }

        protected double[] getBounds()  //获取包围盒
        {
            try
            {
                return new double[] { ((Extents3d)mEntity.Bounds).MaxPoint.X, ((Extents3d)mEntity.Bounds).MaxPoint.Y, ((Extents3d)mEntity.Bounds).MinPoint.X, ((Extents3d)mEntity.Bounds).MinPoint.Y };
            }
            catch (System.Exception ex)
            {
                
                return new double[] { 0, 0, 0, 0 };
            }
        }

        protected int[] getColor() {
            if (mEntity.Color.ColorMethod == ColorMethod.ByLayer)
                return getLayerColor();
            else 
                return new int[] { mEntity.Color.ColorValue.R, mEntity.Color.ColorValue.G, mEntity.Color.ColorValue.B };
        }

        private int[] getLayerColor() {
            if(mLayer != null)
                return new int[] { mLayer.Color.ColorValue.R, mLayer.Color.ColorValue.G, mLayer.Color.ColorValue.B };

            return null;
        }

        protected List<Dictionary<string, object>> getLineType(ObjectId ltId ,Transaction inTr, ObjectId layerId, ObjectId BlockId)
        {
            List<Dictionary<string, object>> lineTypeList = new List<Dictionary<string, object>> { };

            ObjectId lineTypeId = ltId;
            LinetypeTableRecord linetypeTableRecord = (LinetypeTableRecord)inTr.GetObject(ltId, OpenMode.ForRead);

            if ((linetypeTableRecord.Name == "BYLAYER" || linetypeTableRecord.Name == "ByLayer") && mLayer != null)
            {
                LayerTableRecord layer = (LayerTableRecord)mTransaction.GetObject(layerId, OpenMode.ForRead);
                lineTypeId = layer.LinetypeObjectId;
            }

            if (lineTypeId == null)
                return null;

            lineTypeList = getLineTypeData.getLineTypeDataById(mTransaction, lineTypeId);
            return lineTypeList;
        }

        protected void getLineWeight() {
            lineWeight = (int)mEntity.LineWeight;

            // bylayer            
            if (lineWeight == -1 && mLayer != null) {
                lineWeight = (int)mLayer.LineWeight;
            }
        }
    }
}
